/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.de.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.de.entity.AttendanceDe;
import com.thinkgem.jeesite.modules.de.service.AttendanceDeService;

/**
 * 德国考勤Controller
 * @author hui.li
 * @version 2016-09-30
 */
@Controller
@RequestMapping(value = "${adminPath}/de/attendanceDe")
public class AttendanceDeController extends BaseController {

	@Autowired
	private AttendanceDeService attendanceDeService;
	
	@ModelAttribute
	public AttendanceDe get(@RequestParam(required=false) String id) {
		AttendanceDe entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = attendanceDeService.get(id);
		}
		if (entity == null){
			entity = new AttendanceDe();
		}
		return entity;
	}
	
	@RequiresPermissions("de:attendanceDe:view")
	@RequestMapping(value = {"list", ""})
	public String list(AttendanceDe attendanceDe, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AttendanceDe> page = attendanceDeService.findPage(new Page<AttendanceDe>(request, response), attendanceDe); 
		model.addAttribute("page", page);
		return "modules/de/attendanceDeList";
	}

	@RequiresPermissions("de:attendanceDe:view")
	@RequestMapping(value = "form")
	public String form(AttendanceDe attendanceDe, Model model) {
		model.addAttribute("attendanceDe", attendanceDe);
		return "modules/de/attendanceDeForm";
	}

	@RequiresPermissions("de:attendanceDe:edit")
	@RequestMapping(value = "save")
	public String save(AttendanceDe attendanceDe, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, attendanceDe)){
			return form(attendanceDe, model);
		}
		attendanceDeService.save(attendanceDe);
		addMessage(redirectAttributes, "保存考勤成功");
		return "redirect:"+Global.getAdminPath()+"/de/attendanceDe/?repage";
	}
	
	@RequiresPermissions("de:attendanceDe:edit")
	@RequestMapping(value = "delete")
	public String delete(AttendanceDe attendanceDe, RedirectAttributes redirectAttributes) {
		attendanceDeService.delete(attendanceDe);
		addMessage(redirectAttributes, "删除考勤成功");
		return "redirect:"+Global.getAdminPath()+"/de/attendanceDe/?repage";
	}
	
	/**
     * 导出考勤数据
     * @param user
     * @param request
     * @param response
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("de:attendanceDe:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile( HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes,AttendanceDe attendanceDe) {
        try {
            String fileName = "德国考勤数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            //Page<User> page = systemService.findUser(new Page<User>(request, response, -1), user);
            Page<AttendanceDe> page = attendanceDeService.findPage(new Page<AttendanceDe>(request, response, -1), attendanceDe); 
            new ExportExcel("德国考勤数据", AttendanceDe.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "德国导出考勤失败！失败信息："+e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/de/attendanceDeList/?repage";
    }

}